package com.jdew.thkj.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.request.RequestOptions;
import com.jdew.thkj.R;
import com.jdew.thkj.base.BaseActivity;
import com.jdew.thkj.bean.FansBean;
import com.jdew.thkj.bean.TeanBean;
import com.jdew.thkj.manager.AppConfig;
import com.jdew.thkj.network.RetrofitManager;
import com.jdew.thkj.network.api.RequestApi;
import com.jdew.thkj.utils.MD5Utils;
import com.jdew.thkj.utils.RegexValidateUtils;
import com.jdew.thkj.utils.ToastUtils;
import com.jdew.thkj.utils.WeiboDialogUtils;
import com.jdew.thkj.view.GlideCircleTransform;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import static com.bumptech.glide.request.RequestOptions.bitmapTransform;

/**
 * Created by yangzai on 2018/6/7.
 */

public class FansTeamActivity extends BaseActivity {
    @Bind(R.id.cicle)
    TextView cicle;
    @Bind(R.id.jialian)
    TextView jialian;
    @Bind(R.id.gaoji)
    TextView gaoji;
    @Bind(R.id.fans)
    TextView fans;
    @Bind(R.id.top)
    LinearLayout top;
    @Bind(R.id.yestoday)
    TextView yestoday;
    @Bind(R.id.week)
    TextView week;
    @Bind(R.id.month)
    TextView month;
    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.zhishu_jiaolian)
    TextView zhishuJiaolian;
    @Bind(R.id.zhishu_gaoji)
    TextView zhishuGaoji;
    @Bind(R.id.zhishu_fans)
    TextView zhishuFans;
    @Bind(R.id.team_jl)
    LinearLayout teamJl;
    @Bind(R.id.branch_office)
    TextView branchOffice;
    @Bind(R.id.zhishu_branch_office)
    TextView zhishuBranchOffice;
    @Bind(R.id.logo)
    ImageView logo;
    @Bind(R.id.name)
    TextView name;
    @Bind(R.id.level)
    ImageView level;
    @Bind(R.id.regist_ma)
    TextView registMa;
    @Bind(R.id.fuzhi)
    TextView fuzhi;
    private Dialog mWeiboDialog;

    @Override
    public int bindLayout() {
        return R.layout.activity_fans_team;
    }

    @Override
    public void initView(View view) {
        sp = getSharedPreferences("user", Activity.MODE_PRIVATE);
        editor = sp.edit();
        title.setText("我的团队");
        registMa.setText(sp.getString("newUserCode", ""));
        name.setText("欢迎您:" + sp.getString("userPhone", ""));
        String photoPath = sp.getString("UserImgUrl", "");
        if (photoPath.equals("")) {
            logo.setImageResource((R.mipmap.default_icon));
        } else {
            RequestOptions options = new RequestOptions().priority(Priority.HIGH);
            Glide.with(this).load(photoPath).apply(options).apply(bitmapTransform(new GlideCircleTransform(this))).into(logo);
        }
        back.setVisibility(View.VISIBLE);
        back.setOnClickListener(this);
        zhishuJiaolian.setOnClickListener(this);
        zhishuGaoji.setOnClickListener(this);
        zhishuFans.setOnClickListener(this);
        zhishuBranchOffice.setOnClickListener(this);
        teamJl.setOnClickListener(this);
        fuzhi.setOnClickListener(this);
        GetLevel();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back: {
                finish();
            }
            break;
            case R.id.zhishu_jiaolian: {
                Intent in = new Intent(this, FansActivity.class);
                in.putExtra("type", 0);
                startActivity(in);
            }
            break;
            case R.id.zhishu_gaoji: {
                Intent in = new Intent(this, FansActivity.class);
                in.putExtra("type", 2);
                startActivity(in);
            }
            break;
            case R.id.zhishu_fans: {
                Intent in = new Intent(this, FansActivity.class);
                in.putExtra("type", 1);
                startActivity(in);
            }
            break;
            case R.id.fuzhi: {
                RegexValidateUtils.copyText(this, registMa.getText().toString());
                ToastUtils.getInstance(this).showMessage("复制成功!");
            }
            break;
            case R.id.zhishu_branch_office: {
                Intent in = new Intent(this, FansActivity.class);
                in.putExtra("type", 4);
                startActivity(in);
            }
            break;
            case R.id.team_jl: {
                Intent in = new Intent(this, TuiJianMingXiActivity.class);
                startActivity(in);
            }
            break;
            default:
                break;
        }
    }

    public void GetTeam() {
        mWeiboDialog = WeiboDialogUtils.createLoadingDialog(this);
        String timesamp = RegexValidateUtils.getTime13();
        String numsamp = RegexValidateUtils.getRandom10();
        RetrofitManager.getInstance(AppConfig.BASE_URL).createReq(RequestApi.class).GetTeam(sp.getString("userPhone", ""), RegexValidateUtils.getUniquePsuedoID(), timesamp, numsamp, MD5Utils.getMD5(RegexValidateUtils.ASCIISort(timesamp + numsamp + RegexValidateUtils.getUniquePsuedoID() + sp.getString("SignToken", "") + "UserPhone" + sp.getString("userPhone", ""))).toUpperCase()).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<TeanBean>() {
            @Override
            public void onCompleted() {
                WeiboDialogUtils.closeDialog(mWeiboDialog);
            }

            @Override
            public void onError(Throwable e) {
                WeiboDialogUtils.closeDialog(mWeiboDialog);
            }

            @Override
            public void onNext(TeanBean bean) {
                WeiboDialogUtils.closeDialog(mWeiboDialog);
                if (bean.getCode().equals("0")) {
                    cicle.setText(bean.getData().getTotal() + "人");
                    jialian.setText(bean.getData().getSupers() + "人");
                    gaoji.setText(bean.getData().getSenior() + "人");
                    fans.setText(bean.getData().getOrdinary() + "人");
                    yestoday.setText(bean.getData().getYesterday() + "人");
                    week.setText(bean.getData().getThisweek() + "人");
                    month.setText(bean.getData().getThismonth() + "人");
                    zhishuJiaolian.setText(bean.getData().getSubordinasuper() + "人");
                    zhishuGaoji.setText(bean.getData().getSubordinasenior() + "人");
                    zhishuFans.setText(bean.getData().getSubordinaordinary() + "人");
                    branchOffice.setText(bean.getData().getBranchOffice() + "人");
                    zhishuBranchOffice.setText(bean.getData().getDirectlyBranchOffice() + "人");
                }
            }
        });
    }

    public void GetLevel() {
        String timesamp = RegexValidateUtils.getTime13();
        String numsamp = RegexValidateUtils.getRandom10();
        RetrofitManager.getInstance(AppConfig.BASE_URL).createReq(RequestApi.class).GetLevel(sp.getString("userPhone", ""), RegexValidateUtils.getUniquePsuedoID(), timesamp, numsamp, MD5Utils.getMD5(RegexValidateUtils.ASCIISort(timesamp + numsamp + RegexValidateUtils.getUniquePsuedoID() + sp.getString("SignToken", "") + "UserPhone" + sp.getString("userPhone", ""))).toUpperCase()).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<JSONObject>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                Log.e("sssssssssssssss", e.toString());
            }

            @Override
            public void onNext(JSONObject json) {
                if (json.getString("message").equals("铜牌教练")) {
                    level.setImageResource(R.mipmap.lv4);
                } else if (json.getString("message").equals("银牌教练")) {
                    level.setImageResource(R.mipmap.lv5);
                } else if (json.getString("message").equals("分公司")) {
                    level.setImageResource(R.mipmap.lv6);
                } else if (json.getString("message").equals("股东")) {
                    level.setImageResource(R.mipmap.lv7);
                } else if (json.getString("message").equals("超级教练")) {
                    level.setImageResource(R.mipmap.lv3);
                } else if (json.getString("message").equals("普通惠员")) {
                    level.setImageResource(R.mipmap.lv1);
                } else if (json.getString("message").equals("高级惠员")) {
                    level.setImageResource(R.mipmap.lv2);
                } else if (json.getString("message").equals("预备子公司")) {
                    level.setImageResource(R.mipmap.lv8);
                }
                editor.putString("level", json.getString("message"));
                editor.apply();
                GetTeam();
            }
        });
    }
}
